/**
 * Created by no1 on 2014/8/29.
 */
'use strict';
var F = require('../common/function');
var o2oCommMan = require('../common/o2oman');
var C = require('../config/index');
var md5 = require('md5');

module.exports = function (app) {
  var manApiPre = C.manApiPre;

  /*
   @todo API01 添加系统基本权限
   act 1:向系统添加权限
   act 2:获取所有权限列表
   */
  app.post(manApiPre + '/:apiVer/permission', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1, 2]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.premission(this);
    }
  });

  /*
   @todo API02 模糊查询获取商品名称
   */
  app.post(manApiPre + '/:apiVer/getWareByName', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.getWareByName(this);
    }
  });
  /*
   @todo API03 角色相关API
   act  1:添加角色；2：更新角色；3：某商家的角色列表；4：角色权限详细
   */
  app.post(manApiPre + '/:apiVer/roleOperation', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1, 2, 3, 4]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.roleOperation(this);
    }
  });

  /*
   @todo API04 用户相关API
   act  1:添加用户；2：更新用户；3：某商家的用户列表；4：用户权限详细；5:根据手机号码精确查找用户
   6：模糊查询用户信息（isPaging决定是否返回总页数）
   */
  app.post(manApiPre + '/:apiVer/userOperation', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1, 2, 3, 4, 5, 6, 7]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.userOperation(this);
    }
  });

  /*
   @todo API05 商家管理相关API
   act  1:添加商家；2：编辑商家；3：根据id删除商家；4：绑定商家账号； 5：商家列表；6：商家详情；
   */
  app.post(manApiPre + '/:apiVer/vendorOperation', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1, 2, 3, 4, 5, 6]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.vendorOperation(this);
    }
  });

  /*
   @todo API06 机构管理相关API
   act  1:添加机构；2：修改机构信息；3：根据id删除机构；4：机构详情；5：商家的机构列表；6:根据商家获取所有店长列表；7：添加或绑定店长
   act  8:根据商家id获取所有机构的省市列表
   */
  app.post(manApiPre + '/:apiVer/organizationAdd', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.organizationOperation(this);
    }
  });

  app.post(manApiPre + '/:apiVer/organizationUpdate', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [2]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.organizationOperation(this);
    }
  });

  app.post(manApiPre + '/:apiVer/organizationOperation', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.organizationOperation(this);
    }
  });
  /** API07 项目管理相关API
   * act 1:商家详情中的项目列表;
   * */
  app.post(manApiPre + '/:apiVer/wareAdd', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.wareAdd(this);
    }
  });
  app.post(manApiPre + '/:apiVer/wareUpdate', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.wareUpdate(this);
    }
  });

  app.post(manApiPre + '/:apiVer/wareList', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [2, 3, 4, 5]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.wareList(this);
    }
  });

  app.post(manApiPre + '/:apiVer/wareOperation', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1, 2, 3, 4, 5, 6, 7]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.wareOperation(this);
    }
  });

  /**
   @API07 商家，机构，项目的回调API
   act  1:商家回调；2：机构回调；3：项目回调；
   */
  app.post(manApiPre + '/:apiVer/o2oImgCallback', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1, 2, 3]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.o2oImgCallback(this);
    }
  });

  /**
   @API08 图片上传API
   */
  app.post(manApiPre + '/:apiVer/getSignForO2O', function*() {
    this.foro2o = true;
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.getSignForO2O(this);
    }
  });

  /**
   @API09 优惠券相关API
   */
  app.post(manApiPre + '/:apiVer/couponOperation', function*() {
      this.foro2o = true;
      this.I = yield F.Init(this, [1,2,3,4,5,6,7,8]);
      if (this.I.errors) {
        this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
      } else {
        let o2oCommMan = require('../common/o2o/coupon/coupon');
        this.jsonp = yield o2oCommMan.couponOperation(this);
      }
    }
  );

  /**
   * @API10 订单相关API
   */
  app.post(manApiPre+'/:apiVer/orderOperation',function*(){
    this.foro2o = true;
    this.I = yield F.Init(this, [1,2,3,4,5,6,7]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield o2oCommMan.orderOperation(this);
    }
  });

}